clear
set more off

tempfile main bootsave 

insheet using oxy_totals_all.csv, comma

* construct the dummies used in analysis
xi i.month
keep if year>=2006
gen trend=12*(year-2004)+month
gen kgs=(kgs_oxy/(covered_people/1000))
qui save `main' , replace 

postfile bskeep trend ssec Fval using quadratic_spline_rx_results_1, replace


forvalues b = 49/108 { 
	display `b'
	use `main', replace 
	gen z=trend-`b'
	gen post=trend>=`b'

	gen zpost1=post*z
	gen zpost2=zpost1*zpost1
	gen zpre1=(1-post)*z
	gen zpre2=zpre1*zpre1

	reg kgs zpre1 zpre2 zpost1 zpost2
	local ssec=e(rss)
	test (zpre1=zpost1)(zpre2=zpost2)
	local Fval = r(F)
	
	post bskeep (`b') (`ssec') (`Fval') 
	
}
postclose bskeep
clear
use quadratic_spline_rx_results_1
sort trend
save, replace
clear

use `main', replace
sort trend
merge 1:1 trend using quadratic_spline_rx_results_1
egen minssec=min(ssec)
gen mc=minssec==ssec
egen tc=max(mc*trend)

gen z=trend-tc
gen post=trend>=tc
gen zpost1=post*z
gen zpost2=zpost1*zpost1
gen zpre1=(1-post)*z
gen zpre2=zpre1*zpre1
reg kgs zpre1 zpre2 zpost1 zpost2
test (zpre1=zpost1) (zpre2=zpost2)
predict countsp
drop z post zpost* zpre*

keep year month trend ssec tc Fval
outsheet using quadratic_spline_rx_counts_1.csv, comma replace







